{% extends 'dashboard/base.html' %}

{% block title %}分类管理 - 巧巧点餐管理后台{% endblock %}

{% block content %}
<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-gray-800">分类管理</h1>
    <a href="{% url 'dashboard:category_add' %}" class="btn btn-primary btn-sm">
        <i class="fas fa-plus fa-sm"></i> 添加分类
    </a>
</div>

<div class="card shadow mb-4">
    <div class="card-header py-3">
        <h6 class="m-0 font-weight-bold text-primary">筛选条件</h6>
    </div>
    <div class="card-body">
        <!-- 搜索和筛选 -->
        <form method="get" class="search-form">
            <div class="row">
                <div class="col-md-4 mb-2">
                    <div class="form-group mb-2">
                        <label for="search" class="small">搜索</label>
                        <input type="text" id="search" name="search" class="form-control" value="{{ request.GET.search|default:'' }}" placeholder="分类名称...">
                    </div>
                </div>
                <div class="col-md-4 mb-2">
                    <div class="form-group mb-2">
                        <label for="status" class="small">状态</label>
                        <select id="status" name="status" class="form-control">
                            <option value="">全部</option>
                            <option value="active" {% if request.GET.status == 'active' %}selected{% endif %}>启用</option>
                            <option value="inactive" {% if request.GET.status == 'inactive' %}selected{% endif %}>禁用</option>
                        </select>
                    </div>
                </div>
                <div class="col-md-4 mb-2 d-flex align-items-end">
                    <div class="form-group mb-2 d-flex">
                        <button type="submit" class="btn btn-primary btn-sm mr-2">
                            <i class="fas fa-search fa-sm"></i> 搜索
                        </button>
                        <a href="{% url 'dashboard:category_list' %}" class="btn btn-light btn-sm">
                            <i class="fas fa-redo fa-sm"></i> 重置
                        </a>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

<!-- 分类列表 -->
<div class="card shadow mb-4">
    <div class="card-header py-3">
        <h6 class="m-0 font-weight-bold text-primary">分类列表</h6>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover" width="100%" cellspacing="0">
                <thead>
                    <tr>
                        <th width="5%">#</th>
                        <th width="10%">图标</th>
                        <th width="20%">名称</th>
                        <th width="30%">描述</th>
                        <th width="10%">排序</th>
                        <th width="10%">状态</th>
                        <th width="15%">操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% if categories %}
                        {% for category in categories %}
                        <tr>
                            <td>{{ category.id }}</td>
                            <td class="text-center">
                                {% if category.icon %}
                                    <img src="{{ category.icon.url }}" alt="{{ category.name }}" class="img-thumbnail" style="max-height: 50px;">
                                {% elif category.icon_url %}
                                    <img src="{{ category.icon_url }}" alt="{{ category.name }}" class="img-thumbnail" style="max-height: 50px;">
                                {% else %}
                                    <span class="badge badge-light">无图标</span>
                                {% endif %}
                            </td>
                            <td class="text-truncate" style="max-width: 150px;">{{ category.name }}</td>
                            <td class="text-truncate" style="max-width: 250px;">{{ category.description|default:'无描述'|truncatechars:50 }}</td>
                            <td>{{ category.sort_order|default:'0' }}</td>
                            <td>
                                {% if category.is_active %}
                                    <span class="badge badge-success">启用</span>
                                {% else %}
                                    <span class="badge badge-danger">禁用</span>
                                {% endif %}
                            </td>
                            <td>
                                <div class="btn-group">
                                    <a href="{% url 'dashboard:category_edit' category_id=category.id %}" class="btn btn-info btn-sm">
                                        <i class="fas fa-edit fa-sm"></i> 编辑
                                    </a>
                                    <button type="button" class="btn btn-danger btn-sm delete-btn" 
                                            data-toggle="modal" data-target="#deleteModal" 
                                            data-id="{{ category.id }}" 
                                            data-name="{{ category.name }}">
                                        <i class="fas fa-trash fa-sm"></i> 删除
                                    </button>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    {% else %}
                        <tr>
                            <td colspan="7" class="text-center py-4">暂无分类数据</td>
                        </tr>
                    {% endif %}
                </tbody>
            </table>
        </div>

        <!-- 分页 -->
        {% if categories.has_other_pages %}
        <div class="mt-4">
            <nav aria-label="分页导航">
                <ul class="pagination justify-content-center">
                    {% if categories.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ categories.previous_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}" aria-label="上一页">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    {% else %}
                    <li class="page-item disabled">
                        <span class="page-link" aria-hidden="true">&laquo;</span>
                    </li>
                    {% endif %}

                    {% for i in categories.paginator.page_range %}
                        {% if categories.number == i %}
                        <li class="page-item active">
                            <span class="page-link">{{ i }}</span>
                        </li>
                        {% elif i > categories.number|add:'-3' and i < categories.number|add:'3' %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ i }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">{{ i }}</a>
                        </li>
                        {% endif %}
                    {% endfor %}

                    {% if categories.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ categories.next_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}" aria-label="下一页">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                    {% else %}
                    <li class="page-item disabled">
                        <span class="page-link" aria-hidden="true">&raquo;</span>
                    </li>
                    {% endif %}
                </ul>
            </nav>
        </div>
        {% endif %}
    </div>
</div>

<!-- 删除确认模态框 -->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="deleteModalLabel">确认删除</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p>您确定要删除分类 <strong id="categoryName"></strong> 吗？</p>
                <p class="text-danger"><i class="fas fa-exclamation-triangle mr-1"></i> 此操作不可逆，删除分类将同时删除该分类下的所有菜品。</p>
            </div>
            <div class="modal-footer">
                <form id="deleteForm" method="post" action="">
                    {% csrf_token %}
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-danger">确认删除</button>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    $(document).ready(function() {
        // 删除确认
        $('#deleteModal').on('show.bs.modal', function (event) {
            const button = $(event.relatedTarget);
            const categoryId = button.data('id');
            const categoryName = button.data('name');
            
            $('#categoryName').text(categoryName);
            $('#deleteForm').attr('action', '/dashboard/categories/delete/' + categoryId + '/');
        });
    });
</script>
{% endblock %} 